﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using ScrumFactory.Composition.ViewModel;
using ScrumFactory.Services;
using ScrumFactory.Composition;
using System.ComponentModel;

namespace ScrumFactory.GooglePlugin.ViewModel {

    [Export(typeof(IPluginCommand))]
    public class GoogleTaskAutoAddCommand : IPluginCommand, INotifyPropertyChanged {

        public event PropertyChangedEventHandler PropertyChanged;

        private IDialogService dialogs;

        [ImportingConstructor]
        public GoogleTaskAutoAddCommand([Import] IDialogService dialogs) {
            
            this.dialogs = dialogs;

            OnClickCommand = new DelegateCommand<object>(AutoAddTaskToGoogle);
        }

        public int DisplayOrder {
            get {
                return 110;
            }
        }

        public System.Windows.Input.ICommand OnClickCommand {
            get;
            set;
        }

        public string MenuItemHeader {
            get {
                return Properties.Resources.Always_add;
            }
        }

        public string MenuItemIconUrl {
            get {
                return "pack://application:,,,/ScrumFactory.GooglePlugin;component/Images/gtask.png";
            }
        }

        public bool IsCheckeable {
            get {
                return true;
            }
        }

        public bool IsChecked {
            get {
                return Properties.Settings.Default.AutoAddToGoogleTasks;
            }
            set {
                Properties.Settings.Default.AutoAddToGoogleTasks = value;
                Properties.Settings.Default.Save();
                OnPropertyChanged("IsChecked");
            }
        }

        public string ContainerViewModelClassName {
            get {
                return "ScrumFactory.Tasks.ViewModel.TaskViewModel";
            }
        }
        
        private void AutoAddTaskToGoogle(dynamic o) {
            
            System.Windows.MessageBoxResult r =
                dialogs.ShowMessageBox(
                    Properties.Resources.Automatic_add_task,
                    Properties.Resources.Always_add_your_tasks_to_Google,                
                    System.Windows.MessageBoxButton.YesNo,
                    "pack://application:,,,/ScrumFactory.GooglePlugin;component/Images/gtask.png");

            if (r == System.Windows.MessageBoxResult.No)
                IsChecked = false;
            else
                IsChecked = true;
        }

            /// <summary>
        /// Called when a property has changed.
        /// </summary>
        /// <param name="property">The property.</param>
        protected void OnPropertyChanged(string property) {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}
